'use strict';

const {Sequelize,DataTypes} = require('sequelize');

const sequelize = new Sequelize('qainfo','postgres','seventeen123',{
    host:'47.115.176.175',
    dialect:'postgres'
});


function defineModel(tablename,obj){
    let porp = {};

    porp.id = {
        type: DataTypes.BIGINT,
        primaryKey: true,
        autoIncrement: true,
    };

    for(let item in obj){
        porp[item] = obj[item];
    };

    porp.isActive = {
        type : DataTypes.BIGINT,
        allowNull : false,
        defaultValue:1 //1为可用问卷 0为过期问卷, 1为活跃用户 0为冻结用户
    };

    porp.isDelete = {
        type : DataTypes.BIGINT,
        allowNull : false,
        defaultValue:0 //1为已删除问卷 1为已删除用户 
    };

    porp.createdAt = {
        type : DataTypes.BIGINT,
        allowNull : false
    };

    porp.updatedAt = {
        type: DataTypes.BIGINT,
        allowNull: false
    };

    porp.remarks = {
        type: DataTypes.STRING(800),
        allowNull: true
    };

    let User = sequelize.define(tablename,porp,{
        tablename : tablename,
        timestamps: false,
        hooks: {
            beforeValidate: function (obj) {
                let time = Date.now();

                if (obj.isNewRecord) {
                    obj.createdAt = time;
                    obj.updatedAt = time;
                    
                } else {
                    obj.updatedAt = time;
                    
                }
            }
        }
    
    });

    return User;
}

let obj = {
    defineModel,
    sequelize,
    DataTypes
}

module.exports = obj;